<?php 
namespace app\mall\model;
use think\Model;
use think\Db;
class MaterialStockLog extends Model{
	protected $pk = 'id';
	protected $table = 'ys_material_stock_log';
	protected $field = true;        //自动忽略不存在的字段

	private function update_order($da){
    	$store = $da['store_id'];
    	$material= $da['material_id'];
    	$res_num = $da['res_num'];      //实际申请数
        $del_num = $da['del_num'];      //申请数
    	$stock = $da['stock'];		//原料库存，原始库存
        $material_type = $da['material_type'];
        foreach($material as $k => $v){
            $data[$v]['material_id'] = $v;
            $data[$v]['store_id'] = $store[$k];
            $data[$v]['sign'] = 2;
            $data[$v]['type'] = 2;
            $data[$v]['material_type'] = $material_type[$k];
            !isset($data[$v]['res_num'])?$data[$v]['res_num'] = 0:$data[$v]['res_num'];      //实际发货数
            $data[$v]['res_num'] += $res_num[$k];        //累计发货数
            !isset($data[$v]['del_num'])?$data[$v]['del_num'] = 0:$data[$v]['del_num'];
            $data[$v]['del_num'] += $del_num[$k];
            $data[$v]['dif_num'] = $data[$v]['res_num'] - $data[$v]['del_num'];     //差异数
            $data[$v]['cur_num'] = $stock[$k] - $data[$v]['res_num'];
            $data[$v]['remark'] = "出库".$data[$v]['res_num'];
            $data[$v]['addtime'] = time();
        }
    	return $data;
    }	
    /**
     * 总店库存日志
     * @return [type] [description]
     */
    public function save_order($data){
    	$da = $this->update_order($data);		//处理数
    	$result = $this->saveall($da);			//保存所有数据
    	if($result!==false){
    		return ['code'=>1,'mag'=>'保存成功'];
    	}else{
    		return ['code'=>0,'msg'=>'保存失败'];
    	}
    }
}